package com.xiaoyi.intentsdklibrary.Utils;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager;
import android.graphics.drawable.Icon;
import android.os.Build;
import com.xiaoyi.intentsdklibrary.Intent.Constants;

public class ShortCutUtils {
    public static void addShortcut(Context context, String str, String str2, int i, String str3) {
        if (Build.VERSION.SDK_INT >= 26) {
            addShortCut(context, str, str2, i, str3);
            return;
        }
        try {
            Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
            intent.putExtra("duplicate", false);
            intent.putExtra("android.intent.extra.shortcut.NAME", str2);
            intent.putExtra("android.intent.extra.shortcut.ICON_RESOURCE", Intent.ShortcutIconResource.fromContext(context, i));
            Intent intent2 = new Intent("android.intent.action.MAIN");
            intent2.putExtra("action", str3);
            intent2.addCategory("android.intent.category.LAUNCHER");
            intent.putExtra("android.intent.extra.shortcut.INTENT", intent2);
            context.sendBroadcast(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void addShortCut(Context context, String str, String str2, int i, String str3) {
        ShortcutManager shortcutManager = (ShortcutManager) context.getSystemService(Constants.TYPE_CONTROL_SHORTCUT);
        if (shortcutManager.isRequestPinShortcutSupported()) {
            Intent intent = new Intent();
            intent.setAction("android.intent.action.VIEW");
            intent.putExtra("action", str3);
            shortcutManager.requestPinShortcut(new ShortcutInfo.Builder(context, str).setIcon(Icon.createWithResource(context, i)).setShortLabel(str2).setIntent(intent).build(), PendingIntent.getBroadcast(context, 0, new Intent(str3), 134217728).getIntentSender());
        }
    }
}
